package com.nys.cqhttp.message.factory.impl;

import cn.hutool.http.HttpUtil;
import com.nys.cqhttp.constant.CommonConstant;
import com.nys.cqhttp.mapper.*;
import com.nys.cqhttp.message.factory.IMessageHandler;
import com.nys.cqhttp.model.*;
import com.nys.cqhttp.param.Context;
import com.nys.cqhttp.param.MessageParam;
import com.nys.cqhttp.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * DirectImageUrlMessageHandler
 *
 * @author nysheng
 * @version 1.0.0
 * @description 二次元
 * @date 2021/12/28 19:53
 */
@Slf4j
@Component
public class TestMessageHandler implements IMessageHandler {
  @Autowired private YuanShenMapper yuanShenMapper;
  @Autowired private YuanShenConfigMapper yuanShenConfigMapper;
  @Autowired private MessageLogMapper messageLogMapper;
  @Autowired private SysUserMapper sysUserMapper;
  @Autowired private UserConfigMapper userConfigMapper;

  //  private static final String TEMP_FILE = "D:/img/jk";
  //  private static final String TEMP_FILE_PATH = "D:/img/";

  private static TestMessageHandler wjMessageHandler;
  private static String at = "[CQ:at,qq=";
  private static String at1 = "]";

  @PostConstruct
  public void init() throws Exception {
    wjMessageHandler = this;
    yuanShenMapper = this.yuanShenMapper;
    sysUserMapper = this.sysUserMapper;
    userConfigMapper = this.userConfigMapper;
    yuanShenConfigMapper = this.yuanShenConfigMapper;
    messageLogMapper = this.messageLogMapper;
  }

  @Override
  public List<String> listHandler(Massage messagePojo, Context context) {
    return Arrays.asList(handler(messagePojo, context));
  }

  @Override
  public String handler(Massage messagePojo, Context context) {
    try {
      MessageParam messageParam = context.getMessageParam();
      String message = messageParam.getMessage();
      String[] split = message.split("-");
      String searchUrl = "http://music.163.com/api/search/get/web?csrf_token=hlpretag=&hlposttag=&s=" + URLEncoder.encode(split[1], "UTF-8") + "&type=1&offset=0&total=true&limit=1";
      // 搜索网易云音乐的 API 地址

      URL url = new URL(searchUrl);
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
      conn.setRequestMethod("GET");
      conn.setRequestProperty("User-Agent", "Mozilla/5.0");
      conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
      conn.setRequestProperty("Accept-Charset", "UTF-8");
      conn.setDoOutput(true);

      BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
      StringBuilder sb = new StringBuilder();
      String output;
      while ((output = br.readLine()) != null) {
        sb.append(output);
      }

      String result = sb.toString();
      String id =
              result.substring(result.indexOf("\"id\":")+5, result.indexOf(",\"name\":"));
      return "[CQ:music,type=163,id="+id+"]";
    } catch (Exception e) {
      return null;
    }
  }
}
